package com.tz.online.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="BS_ITEM")
public class OrderItem {
	private Long id;   //商品明细ID标识
	
	//订单明细中book核心属性，采用冗余的方式添加
	private Long bookId;
	private String bookName; //书名
	private int count;   //购买数量
	private double price; //单价
	private String imageUrl;  //封面图片
	
	private Order order;    //此明细所属的订单
	
	@Id
	@GeneratedValue(generator="item_id",strategy=GenerationType.SEQUENCE)
	@SequenceGenerator(name="item_id",sequenceName="bs_item_id")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column
	public Long getBookId() {
		return bookId;
	}
	public void setBookId(Long bookId) {
		this.bookId = bookId;
	}
	
	@ManyToOne
	@JoinColumn(name="ORDER_ID",nullable=false)
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	
	@Column
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	@Column
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	@Column
	public double getPrice() {
		return price;
	}
	public void setPrice(double unitPrice) {
		this.price = unitPrice;
	}
	@Column
	public String getImageUrl() {
		return imageUrl;
	}
	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	
	public OrderItem() {
		super();
	}
	
	public OrderItem(Long bookId, String bookName, String imageUrl, double price, int count) {
		super();
		this.bookId = bookId;
		this.bookName = bookName;
		this.count = count;
		this.price = price;
		this.imageUrl = imageUrl;
	}
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("OrderItem [id=").append(id).append(", bookId=").append(bookId).append(", bookName=")
				.append(bookName).append(", count=").append(count).append(", price=").append(price)
				.append(", imageUrl=").append(imageUrl).append("]");
		return builder.toString();
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((bookId == null) ? 0 : bookId.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OrderItem other = (OrderItem) obj;
		if (bookId == null) {
			if (other.bookId != null)
				return false;
		} else if (!bookId.equals(other.bookId))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
}
